package com.vanke.devops.domain.manager.shangyi.impl;

import com.google.common.collect.Maps;
import com.vanke.devops.domain.manager.shangyi.ShangYiApi;
import com.vanke.devops.domain.manager.shangyi.ShangYiManager;
import com.vanke.devops.domain.manager.shangyi.util.HttpRequest;
import com.vanke.devops.domain.manager.shangyi.util.XStreamTransformer;
import com.vanke.devops.domain.properties.ShangYiProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author 陈景阳
 * @since 2018/6/5
 */
@Service
@EnableConfigurationProperties(ShangYiProperties.class)
public class ShangYiManagerImpl implements ShangYiManager {

    @Autowired
    private ShangYiProperties shangYiProperties;

    @Override
    public Integer getBalance(){
        String url = shangYiProperties.getHost() + "/"+ ShangYiApi.GET_BALANCE;
        Map<String,String> param = Maps.newHashMap();
        param.put("sn",shangYiProperties.getSn());
        param.put("pwd",shangYiProperties.getPassword());
        String xml = HttpRequest.sendPost(url,param);
        String total = XStreamTransformer.fromXml(String.class, xml);
        return Integer.valueOf(total.trim());
    }
}
